//
//  StoreDynamicListViewController.m
//  BaseProject
//
//  Created by 王洋 on 2020/7/30.
//  Copyright © 2020 王洋. All rights reserved.
//
#import "ZZStarView.h"

#import "StoreDetailEvaluationCell.h"
#import "StoreDynamicSectionHeader.h"
#import "StoreDynamicDetailViewController.h"

#import "StoreDynamicListViewController.h"

@interface StoreDynamicListViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UIImageView *storeIcon;

@property (weak, nonatomic) IBOutlet UILabel *storeName;

@property (weak, nonatomic) IBOutlet UILabel *descLab;

@property (weak, nonatomic) IBOutlet UILabel *starCountLab;

@property (weak, nonatomic) IBOutlet UIView *starBGView;

@property (weak, nonatomic) IBOutlet UILabel *dynamicCountLab;

@property (weak, nonatomic) IBOutlet UITableView *tableview;

@property (nonatomic, strong) NSMutableArray<CommonDynamicModel *>* dataArray;

@property (nonatomic, strong) ZZStarView *starView;

@end

@implementation StoreDynamicListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initView];
    [self request_DynamicList];
}

- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.01 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        [self.storeIcon pp_clipCorners:UIRectCornerAllCorners radius:6];
    });
}

- (void)initView {
    [self.starBGView addSubview:self.starView];
    [self.starView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.mas_equalTo(0);
    }];
    
    self.tableview.delegate = self;
    self.tableview.dataSource = self;
    self.tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableview.backgroundColor =  UIColorHex(0xf1f1f1);
    
    self.tableview.showsHorizontalScrollIndicator = NO;
    self.tableview.showsVerticalScrollIndicator = NO;
    
    self.tableview.estimatedSectionHeaderHeight = 0;
    self.tableview.estimatedSectionFooterHeight = 0;
    self.tableview.estimatedRowHeight = 0;
    
    [_tableview registerNib:[UINib nibWithNibName:@"StoreDetailEvaluationCell" bundle:nil] forCellReuseIdentifier:@"StoreDetailEvaluationCellID"];

    [_tableview registerNib:[UINib nibWithNibName:@"StoreDynamicSectionHeader" bundle:nil] forHeaderFooterViewReuseIdentifier:@"StoreDynamicSectionHeader"];

    self.starBGView.hidden = YES;
    self.starCountLab.hidden = YES;
    self.dynamicCountLab.hidden = YES;
    self.descLab.hidden = YES;
}

- (IBAction)goImAction:(id)sender {
    
    [[TT_IMContactManager manager] ImContactWithStoreId:self.storeModel.sellerId];
}


#pragma mark UITableviewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataArray.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 0;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 120;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return [StoreDynamicSectionHeader calculateHeightWithModel:self.dataArray[section]];
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    StoreDynamicSectionHeader *headView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"StoreDynamicSectionHeader"];
    headView.contentView.backgroundColor = UIColor.whiteColor;

    [headView fillHeadWithModel:self.dataArray[section]];
    WEAKSELF
    [headView addGestureTapEventHandle:^(id  _Nonnull sender, UITapGestureRecognizer * _Nonnull gestureRecognizer) {
       
        StoreDynamicDetailViewController *vc = [[StoreDynamicDetailViewController alloc] init];
        vc.sdyId = weakSelf.dataArray[section].sdyId;
        [weakSelf.navigationController pushViewController:vc animated:YES];
    }];
    
    return  headView;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    StoreDetailEvaluationCell *cell = [tableView dequeueReusableCellWithIdentifier:@"StoreDetailEvaluationCellID"];
    cell.selectionStyle =UITableViewCellSelectionStyleNone;
    return cell;
}

- (void)request_DynamicList {
    
    NSMutableDictionary *dic = @{}.mutableCopy;

    [dic setValue:@(1) forKey:@"pageNum"];
    [dic setValue:@(20) forKey:@"pageSize"];
    
    NSDictionary *t = @{}.mutableCopy;
    [t setValue:self.storeId forKey:@"sdySellerId"];
    [dic setValue:t forKey:@"t"];

    NSString *url = [NSString stringWithFormat:@"%@%@",TT_Global_Api_Domain,API_StoreDynamic];
    
    [SVProgressHUD showWithStatus:@"正在加载"];
    WEAKSELF
    [TTHttpManager postRequestWithUrl:url TimeOut:10 PostData:dic FinishBlock:^(id  _Nonnull data) {
        
        [SVProgressHUD dismiss];
        if ([data isKindOfClass:[NSDictionary class]]) {

            //请求成功
            NSInteger code = [[data valueForKey:@"code"] integerValue];
            if (code == 10000) {
                NSDictionary *resultDic = [data valueForKey:@"data"];
                if (![resultDic isKindOfClass:[NSDictionary class]]) {
                    return;
                }
                NSArray *SellerDynamicList = [resultDic valueForKey:@"SellerDynamicList"];
                
                NSDictionary *seller = [resultDic valueForKey:@"seller"];
                weakSelf.storeModel = [CommonSellerModule modelWithDictionary:seller];
                
                self.dataArray = @[].mutableCopy;
                
                if ([SellerDynamicList isKindOfClass:[NSArray class]]) {
                    for (int i = 0; i < SellerDynamicList.count; i++) {
                        NSDictionary *SellerDynamicListDic = SellerDynamicList[i];
                        CommonDynamicModel *dynamicModel = [CommonDynamicModel modelWithDictionary:SellerDynamicListDic];
                        
                        NSArray *imageUrls = [SellerDynamicListDic valueForKey:@"imageUrls"];
                        dynamicModel.imageUrls = @[].mutableCopy;
                        if ([imageUrls isKindOfClass:[NSArray class]]) {
                            
                            for (int j = 0; j < imageUrls.count; j++) {
                                NSDictionary *imageDic = imageUrls[j];
                                CommonImageModel *imgModel = [CommonImageModel modelWithDictionary:imageDic];
                                if ([imgModel.type isEqualToString:@"dtimg"]) {
                                    NSString *url = [TT_OSSManager reSourceUrl:imgModel.url type:BA_resizeType_plus];
                                    imgModel.url = url;
                                    [dynamicModel.imageUrls addObject:imgModel];
                                }
                            }
                        }
                        
                        [weakSelf.dataArray addObject:dynamicModel];
                    }
                }
                                
                weakSelf.descLab.text = [NSString stringWithFormat:@"%ld条 %ld关注",(long)weakSelf.storeModel.sellerCount,(long)weakSelf.storeModel.collectionCount];
                weakSelf.storeName.text = weakSelf.storeModel.sellerName;
                
                NSString *url = [TT_OSSManager reSourceUrl:weakSelf.storeModel.logoUrl type:BA_resizeType_plus];
                [weakSelf.storeIcon sd_setImageWithURL:[NSURL URLWithString:url] placeholderImage:[UIImage imageNamed:@"placeholder"]];
                weakSelf.dynamicCountLab.text = [NSString stringWithFormat:@"动态%lu",(unsigned long)weakSelf.dataArray.count];
                
                weakSelf.starView.grade = weakSelf.storeModel.sellerEvaluateStar;

                if (weakSelf.storeModel.sellerEvaluateStar > 0) {
                    self.starCountLab.text = [NSString stringWithFormat:@"%.1f",weakSelf.storeModel.sellerEvaluateStar];
                } else {
                    self.starCountLab.text = @"暂无评分";
                }
                
                [weakSelf.tableview reloadData];
            }
        }
        
    } FailedBlock:^(NSError * _Nonnull error) {
        
        [SVProgressHUD dismiss];
    }];
    
}

- (ZZStarView *)starView {
    
    if (!_starView) {

        _starView = [[ZZStarView alloc] initWithImage:[UIImage imageNamed:@"daxing_nor"] selectImage:[UIImage imageNamed:@"daxing_sel"] starWidth:12 starHeight:12 starMargin:2 starCount:5 callBack:^(CGFloat userGrade, CGFloat finalGrade) {
        }];
        //默认值, 可以不写, 用户可选分值范围是0.5的倍数.(建议在设置分值之前确定此值)
        _starView.sublevel = 0.5;
        //设置分值, 可以不写, 默认显示0分.(self.params是UIViewController在ZZRouter中扩展的属性, 包含了所有参数)
        _starView.grade = 0.0;
        //最小值, 默认0.5可以不写, 用户可以设置的最低分值.
        _starView.miniGrade = 0;
        _starView.frame = CGRectMake(0, 0, 70, 14);

    }
    return _starView;
}


@end
